package com.travel.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.travel.entity.ScenicOrder;
import com.travel.service.ScenicOrderService;
import com.travel.service.impl.ScenicOrderServiceImpl;
import com.travel.util.PageInfo;

/**
 * Servlet implementation class ScenicOrderServlet
 */
@WebServlet("/ScenicOrderServlet")
public class ScenicOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ScenicOrderService scenicOrderService = new ScenicOrderServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScenicOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String op = request.getParameter("op");
		if(op == null) {
			//查询列表
			doFindScenic(request,response);
		}else if ("searchByPage".equals(op)) {
			//分页查询列表
			doFindScenicOrderByPage(request,response);
		}
	}

	private void doFindScenic(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//调用业务层
		List<ScenicOrder> list = scenicOrderService.find();
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter  out= response.getWriter();
		out.print(json);
	}

	private void doFindScenicOrderByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String userName = request.getParameter("keyword");
	
		PageInfo<ScenicOrder> findByPage = scenicOrderService.findByPage(pageNum, pageSize,userName);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(findByPage);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter  out= response.getWriter();
		out.print(json);
	}

	
}
